package com.mikufufu.blog.common.entity;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;

/**
 * 用户鉴权实体类
 * 
 */
@Data
public class SysUser implements UserDetails {

    private static final long serialVersionUID = 1L;

    /**
     * 用户id
     */
    private Integer userId;

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 角色id
     */
    private Integer roleId;

    /**
     * 角色
     */
    private String role;

    /**
     * 权限列表
     */
    private Set<String> permissions;


    public SysUser(Integer userId,String username, String password,Integer roleId, String role, Set<String> permissions) {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.roleId = roleId;
        this.role = role;
        this.permissions = permissions;
    }

    /**
     * Authorities 权限集合 用于存储用户权限信息
     * @return 权限集合
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        GrantedAuthority authority = new SimpleGrantedAuthority("ROLE_" + role);
        authorities.add(authority);
        return authorities;
    }

    /**
     * 判断帐号是否已经过期
     * @return boolean
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 判断帐号是否已被锁定
     * @return boolean
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 判断用户凭证是否已经过期
     * @return boolean
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 判断用户帐号是否已启用
     * @return boolean
     */
    @Override
    public boolean isEnabled() {
        return true;
    }
}
